﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
//using log4net;
using IDML.BusinessObject;

namespace IDML.DataAccess
{
    public class UserDAL
    {
       // public static readonly ILog Log = log4net.LogManager.GetLogger(typeof(UserDAL));
        private DBConnection _dbConnect;

        public UserDAL()
        {
            _dbConnect = new DBConnection();
        }

        public UserBO GetUser(UserBO userBo)
        {
            SqlConnection connection = null;
            DataSet ds = null;
            UserBO user = null;
            try
            {
                connection = _dbConnect.OpenConnection();
                _dbConnect.BeginTransaction();
                SqlCommand selectCommand = new SqlCommand("GetUser", connection);
                selectCommand.CommandType = CommandType.StoredProcedure;
                selectCommand.Parameters.Add("@UserName", SqlDbType.VarChar).Value = userBo.UserName;
                selectCommand.Parameters.Add("@Password", SqlDbType.VarChar).Value = userBo.Password;
                SqlDataAdapter da = new SqlDataAdapter(selectCommand);
                ds = new DataSet();
                _dbConnect.CommitTransaction();
                da.Fill(ds);             
                _dbConnect.CloseConnection();
                user = new UserBO();
                foreach (DataRow row in ds.Tables[0].Rows)
                {
                    user.UserID = int.Parse(row["UserID"].ToString());
                    user.UserName = row["UserName"].ToString();
                    user.FirstName = row["FirstName"].ToString();
                    user.LastName = row["LastName"].ToString();
                    user.RoleId = int.Parse(row["RoleId"].ToString());
                }
                return user;

            }
            catch (Exception ex)
            {
                string error = ex.Message;
                //Log.Error("GetUser", ex);
                _dbConnect.RollbackTransaction();
                _dbConnect.CloseConnection();
            }
            finally
            {
                _dbConnect.CloseConnection();
            }
            return user;
        }

        public DataSet GetUserMenuList(int RoleID,bool IsMenu)
        {
           // List<MenuBO> menuList = new List<MenuBO>();
            SqlConnection connection = null;
            DataSet ds = null;
           // MenuBO menu = null;
            try
            {
                connection = _dbConnect.OpenConnection();
                _dbConnect.BeginTransaction();
                SqlCommand selectCommand = new SqlCommand("RoleMenu_Select", connection);
                selectCommand.CommandType = CommandType.StoredProcedure;
                selectCommand.Parameters.Add("@RoleId", SqlDbType.Int).Value = RoleID;
                selectCommand.Parameters.Add("@IsMenu", SqlDbType.Int).Value = IsMenu;   
                SqlDataAdapter da = new SqlDataAdapter(selectCommand);
                ds = new DataSet();
                _dbConnect.CommitTransaction();
                da.Fill(ds);
              
                _dbConnect.CloseConnection();

                //foreach (DataRow row in ds.Tables[0].Rows)
                //{
                //    menu = new MenuBO();
                //    menu.MenuId = int.Parse(row["MenuId"].ToString());
                //    menu.Text = row["Text"].ToString();                   
                //    menu.Description = row["Description"].ToString();
                //    menu.NavigateUrl = row["NavigateUrl"].ToString();              
                //    menu.ParentId = row["ParentId"] == DBNull.Value ? (int?)null : int.Parse(row["ParentId"].ToString());
                //    menu.MenuOrder = row["MenuOrder"] == DBNull.Value ? (short?)null : short.Parse(row["MenuOrder"].ToString());                   
                //    menuList.Add(menu);
                //}
               // return menuList;

            }
            catch (Exception ex)
            {
               // Log.Error("GetMenuList", ex);
                _dbConnect.RollbackTransaction();
                _dbConnect.CloseConnection();
            }
            finally
            {
                _dbConnect.CloseConnection();
            }
            return ds;
        }

    }
}
